<?php
@session_start();
require_once("../connect_db.php");
$arr_month=array();
$arr_income=array();
$arr_outcome=array();
$start=$_GET["month1"];
$stop=$_GET["month2"];
$set=0;
$ostart=$start;
for(;$start<=$stop;$start++,$set++){
$arr_month[$set]=$start;
$arr_income[$set]=0;
$arr_outcome[$set]=0;
}
$sql_order="select MONTH(order_date_regis) as monthIncome,sum(price) as Income from orders,course where orders.course_id=course.course_id and YEAR(order_date_regis)='".($_GET["temp"]-543)."' and 
MONTH(order_date_regis)>='".$ostart."' and MONTH(order_date_regis)<='".$stop."' group by MONTH(order_date_regis)";
$query_order=mysql_query($sql_order);

$sql_orderO="select MONTH(cost_date) as monthOutcome,sum(cost_price) as Outcome from costlearn where 
YEAR(cost_date)='".($_GET["temp"]-543)."' MONTH(cost_date)>='".$ostart."' and MONTH(cost_date)<='".$stop."' group by MONTH(cost_date)";
$query_orderO=mysql_query($sql_orderO);
$r=0;
$sumIncome=0;
$sumOutcome=0;
while($result_order=mysql_fetch_array($query_order)){
$sumIncome+=$result_order["Income"];
$arr_income[$result_order["monthIncome"]-1]=$result_order["Income"];
$r++;
}
$r=0;
while($result_orderO=mysql_fetch_array($query_orderO)){
$sumOutcome+=$result_orderO["Outcome"];
$arr_outcome[$result_orderO["monthOutcome"]-1]=$result_orderO["Outcome"];
$r++;
}

$html='<div style="text-align:center"><h1>รายงานรายรับ-รายจ่าย ประจำปี  '.$_GET["temp"].'</h1><h4>88/9 หมู่ 1 ถนนบ้านนา เลยธนาคารไทยพาณิชย์ (ตลาดสัตหีบ)ต.สัตหีบ อ.สัตหีบ จ.ชลบุรี</h4></div>
<br/>
<br/>

<table style="width:100%" border="1">
<tr><thead ><th>เดือน</th><th>รายรับ</th><th>รายจ่าย</th></thead></tr><tbody>
';
$i=0;
foreach($arr_month as $month){
 $html.='<tr><td>'.monthConv($month).'</td>
 <td style="text-align:right;">'.$arr_income[$i].'</td>
 <td style="text-align:right;">'.$arr_outcome[$i].'</td>
 </tr>';
 $sql_sub_order="select price,CONCAT(std_name,' ',std_lname) As Name,course_name from student,orders,course where 
orders.std_id=student.std_id
and orders.course_id=course.course_id 
and YEAR(order_date_regis)='".($_GET["temp"]-543)."' and 
MONTH(order_date_regis)='".$month."'  ";
$query_sub_order=mysql_query($sql_sub_order);
while($resultsub=mysql_fetch_array($query_sub_order)){
$html.='<tr><td>->'.$resultsub["Name"].'('.$resultsub["course_name"].')'.'</td>
 <td style="text-align:right;">'.$resultsub["price"].'</td>
 <td style="text-align:right;">-</td>
 </tr>';
 $sumIncome+=$resultsub["price"];
}

 $sql_sub_order_o="select CONCAT(car_brand,' ',car_model) As Name,cost_No,cost_price from car,costlearn 
 where car.car_id=costlearn.car_id and
YEAR(cost_date)='".($_GET["temp"]-543)."' and MONTH(cost_date)='".$month."' 
 ";
$query_sub_order_o=mysql_query($sql_sub_order_o);
while($resultsubo=mysql_fetch_array($query_sub_order_o)){
$html.='<tr><td>->'.$resultsubo["Name"].'('.$resultsubo["cost_No"].')'.'</td>
 <td style="text-align:right;">-</td>
 <td style="text-align:right;">'.$resultsubo["cost_price"].'</td>
 </tr>';
 $sumOutcome+=$resultsubo["cost_price"];
}
 $i++;
}
 $html.='<tr><tbody>
	<td>รวม</td>
	<td style="text-align:right">'.$sumIncome.'</td>
	<td style="text-align:right">'.$sumOutcome.'</td>
	</tr>
	<tr ><tbody>
	<td  colspan="2">รวมสุทธิ</td>
	<td style="text-align:right">'.($sumIncome-$sumOutcome).'</td>
	</tbody></tr>
	</tbody></table>';

include("../MPDF57/mpdf.php");
$mpdf=new mPDF();
$mpdf->SetAutoFont();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;

function monthConv($m){
switch($m){
case 1:return "มกราคม";
case 2:return "กุมภาพันธ์";
case 3:return "มีนาคม";
case 4:return "เมษายน";
case 5:return "พฤษภาคม";
case 6:return "มิถุนายน";
case 7:return "กรกฏาคม";
case 8:return "สิงหาคม";
case 9:return "กันยายน";
case 10:return "ตุลาคม";
case 11:return "พฤศจิกายน";
case 12:return "ธันวาคม";
}
}
?>